import {padZero} from "../utils/utils.js";

export class flightRequestParamsFactory {

    constructor(props) {

    }

    static createSearchParam(name) {
        return JSON.stringify([[["H028ib",
            JSON.stringify([name, [1,2,3,5],null,[2],1])
            , null, "generic"]]])

    }

    /**
     *
     * @param {requestParam} flightParams
     * @returns {string}
     */

    static createShoppingParams(flightParams, isMore) {
        let tt = JSON.stringify(
            [
                // protobuf_decoder?[null,null,null,protobuf_decoder]:[],
                [],
                [null, null,
                    flightParams.isTripType ? 1 : 2
                    , null, [],
                    1, // 舱位类型
                    this.createOrderNum(flightParams)
                    , null, null, null, null, null, null,
                    this.createFlightParams(flightParams, null, null),

                    null, null, null, 1],
                0, isMore?1:0, 0, flightParams.cheapest?2:1]
        )
        return JSON.stringify(
            [null,
                tt]
        );
    }

    /**
     *
     * @param {requestParam} flightParams
     * @param {flightInfo} depFlight
     * @returns {string}
     */
    static createShoppingReturnParams(flightParams, depFlight, isMore) {

        return JSON.stringify([null,
            JSON.stringify(
                [
                    [null, depFlight.order],
                    [null, null,
                        flightParams.isTripType ? 1 : 2
                        , null, [], 1,
                        this.createOrderNum(flightParams),
                        null, null, null, null, null, null,
                        this.createFlightParams(flightParams, depFlight, null),

                        null, null, null, 1], 0, isMore?1:0, 0, flightParams.cheapest?2:1
                ]            )

        ]);

    }
    /**
     * 生成乘客数量
     * @param {requestParam} flightParams
     * @returns {*[]}
     */
    static createOrderNum(flightParams) {
        return [
            flightParams.adultSum,
            flightParams.childSum,
            0,
            0
        ];
    }

    /**
     * 生成乘客数量
     * @param {requestParam} flightParams
     * @param gfsFlight 去程信息
     * @param returnFlight 返程信息
     * @returns {*[]}
     */
    static createFlightParams(flightParams, gfsFlight, returnFlight) {


        let res = [];
        res.push([
            [[this.createCityParams(flightParams.startCityParam)]],
            [[this.createCityParams(flightParams.endCityParam)]],
            // molia
            null, 0, null, null, flightParams.formatDate ,null,null,null,null,null,null,null,3
        ]);
        // console.log(1,JSON.stringify(res))
        if (flightParams.isTripType) {
            res.push([
                [[this.createCityParams(flightParams.endCityParam)]],
                [[this.createCityParams(flightParams.startCityParam)]],
                null, 0, null, null, flightParams.formatDateEnd,null,null,null,null,null,null,null,3
            ]);
            // console.log(2,JSON.stringify(res))

        }

        if (gfsFlight === null && returnFlight === null) {
            return res;
        }

        let res2 = [];
        res2.push([
            [[this.createCityParams(flightParams.startCityParam)]],
            [[this.createCityParams(flightParams.endCityParam)]],
            null, 0, null, null, flightParams.formatDate, null,

            this.createLegsParams(gfsFlight)
            // todo
            ,null,null,null,null,null,3
        ]);
        // console.log(3,JSON.stringify(res2))

        if (flightParams.isTripType) {
            if (returnFlight === null) {
                res2.push([
                    [[this.createCityParams(flightParams.endCityParam)]],
                    [[this.createCityParams(flightParams.startCityParam)]],
                    null, 0, null, null, flightParams.formatDateEnd
                    ,   null,null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    3
                ]);
            } else {
                res2.push([
                    [[this.createCityParams(flightParams.endCityParam)]],
                    [[this.createCityParams(flightParams.startCityParam)]],
                    null, 0, null, null, flightParams.formatDateEnd, null,
                    this.createLegsParams(returnFlight),
                    null,
                    null,
                    null,
                    null,
                    null,
                    3
                ]);
            }
        }

        return res2;
    }

    /**
     *
     * @param {cityParam} param
     * @returns {[]*}     */
    static createCityParams(param) {
        if (param.type === 3) {
            return [param.text, 5]
        }
        return [param.code, 0]
    }


    /**
     * 拼接航段信息
     * @param {flightInfo} gfsFlight
     * @returns {[]*}
     */
    static createLegsParams(gfsFlight) {
        let res = gfsFlight.legs.map(leg => {

            return [leg.departureAirportCode,
                formatTime(leg.departureTime),
                leg.landingAirportCode,
                null,
                leg.airlineCode,
                leg.flightNumber]
        })
        return res
    }

    /**
     *  生成 booking 参数
     * @param {requestParam} flightParams
     * @param {flightInfo} depFlight
     * @param {flightInfo} returnFlight
     * @returns {string}
     */
    static createBookingParams(flightParams, depFlight, returnFlight) {
        return JSON.stringify(
            [null,
                JSON.stringify(
                    [
                        [null, returnFlight == null ? depFlight.order : returnFlight.order],
                        [null, null, flightParams.isTripType ? 1 : 2
                            , null, [], 1,
                            this.createOrderNum(flightParams),
                            null, null, null, null, null, null,

                            this.createFlightParams(flightParams, depFlight, returnFlight), null, null, null, 1], null,
                        // 0
                        returnFlight == null ? depFlight.typeCode : returnFlight.typeCode
                    ]
                )]
        );
    }


}


/**
 *
 * @param {Date} time
 * @returns {string}
 */
function formatTime(time) {
    // time = Date.now();
    return `${time.getFullYear()}-${padZero(time.getMonth() + 1)}-${padZero(time.getDate())}`;
}